/*==============================================================================
Description:
==============================================================================*/

#ifndef __TRACTION_H__
#define __TRACTIONL_H__

/***************************************************************************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif

//---------------------------------------------------------------------------
#include "../cfg/prj.h"

#include "../pi/pi.h"
#include "../ramp/ramp.h"
#include "../interpolate/interpolate.h"
#include "../direction_switch/direction_switch.h"
#include "../output/output.h"

//---------------------------------------------------------------------------
// Constant Definitions


//---------------------------------------------------------------------------
// Macros
//
//---------------------------------------------------------------------------
// Global Variable References

typedef enum
{
	STOP = 0,
	CREEP,
	SLOW,
	NORMAL
} SPEED_STATE;
typedef enum
{
	BRAKE_PREPROCESS_NOT_OPRATON = 0,
	BRAKE_PREPROCESS_FORWARK,
	BRAKE_PREPROCESS_REVERSE
} BRAKE_PREPROCESS_STATE;
typedef enum
{
	BRAKE_FORWARK = 0,
	BRAKE_REVERSE
} BRAKE_STATE;
typedef enum 
{
    EXIT_BRAKE_POSITIVE_TO_ZERO=0,
	EXIT_BRAKE_NEGATIVE_TO_ZERO
} EXIT_BRAKE_STATE;
typedef enum
{
	PARMID_NOT_OPRATON = 0,
	PARMID_PIONT1_X,   // 1
	PARMID_PIONT2_X,   // 2
	PARMID_PIONT3_X,   // 3
	PARMID_PIONT1_Y,   // 4
	PARMID_PIONT2_Y,   // 5
	PARMID_PIONT3_Y,   // 6
	PARMID_PI_KP,     // 7
	PARMID_PI_KI,      // 8
	PARMID_READ_ID,   //9
	PARMID_KPNUM,	// 10
	PARMID_K1NUM,	// 11
	PARMID_K2NUM,	// 12
	PARMID_KFNUM,	//13
	PARMID_KPDEN,	//14
	PARMID_KPMIN,	//15
	PARMID_KPMAX,   //16
	PARMID_ENABLE_MANUAL_CTRL_TORQUE,//17
	PARMID_MANUL_TORQUE,//18
	PARMID_ENABLE_MANUL_CTRL_ACC,//19	
	PARMID_MANUL_ACC,     //20
    PARMID_MAX_ACCELERATION,//21	
	PARMID_MAX_DECELERATION,	 //22
	PARMID_MAX_TORQUE_RISE_STEP,//23
	PARMID_MAX_TORQUE_FALL_STEP //24

} ParamID_Type;
typedef struct TRACTION_PARAMETER
{
	U16 Speed_Forward_Max;  /*m/h*/
	U16 Speed_Backward_Max; /*m/h*/
	F32 Acceleration_Max;
	F32 Deceleration_Max;
	F32 Wheel_Radius;
	F32 Gear_Ratio;

	S16 Chip_Temperture_Start;
	S16 Chip_Temperture_End;
	S16 Motor_Temperture_Start;
	S16 Motor_Temperture_End;
	S16 Battery_Voltage_Start;
	S16 Battery_Voltage_End;
	S16 Battery_Soc_Start;
	S16 Battery_Soc_End;
	S16 Torque_max;
	
	F32 Discharge_Torque;
	U16 Discharge_MaxTime;
} TRACTION_PARAMETER;

typedef struct TRACTION_INPUT
{
	S16 Chip_Temperture;
	S16 Motor_Temperture;
	S16 Battery_Voltage;
	U16 Accelerator_Pedal_Ratio;
	U16 Accelerator_Pedal_Ratio_Input;
	U16 Brake_Pedal_Ratio;
	enum direction_switch_state Speed_Direction;
	F32 Motor_Speed_rpm;
	U16 Torque_Enable;
	U16 Speed_State;
	S16 Steer_Angle;
	
	
	S16 Disable_Traction;
	S16 Battery_Soc;
	S16 Seat_Signal;
} TRACTION_INPUT;

typedef struct TRACTION_INNER
{
	F32 Torque_Chip_Temperture_Limit;
	F32 Torque_Motor_Temperture_Limit;
	F32 Torque_Battery_Voltage_Limit;
	F32 Torque_Battery_Soc_Limit;

	F32 Truck_Speed_mph; /*units m/h*/
	F32 Truck_Speed_mps; /*units m/s*/
	F32 Motor_Speed_rpm; /*units rev/m*/
	F32 Truck_Speed_mms;/*units mm/s*/

	F32 Torque_Out; /*Nm*/


	U8	Enter_Brake_First_Flag;
	BRAKE_PREPROCESS_STATE	Enter_Brake_hysteresis_Flg;
	U8  Enter_Brake_Steps_Num;
	F32 Enter_Brake_Torque_Step;	
	U8  Exit_Brake_Update_Param_Flag;	
	U8	Exit_Brake_hysteresis_Flg;
	F32 Exit_Brake_Torque_Step;

    BRAKE_STATE Brake_state_Flag;	
	F32 Brake_Current_Tourque;
	F32 Brake_Torque_Step;
	F32 Brake_Max_Torque;
	U8  Brake_Rember_Torque_Flag;
	U8  Brake_Forwark_Ramp_Flag;
	U8  Brake_Reverse_Ramp_Flag;
	
	F32 Brake_Ramp_UpperLimit;
	F32 Brake_Ramp_LowerLimit;
	F32 Brake_Ramp_Step;
	F32 Brake_Ramp_Half_Step;

	U8  Rember_StsMagnetized_Flag;
	F32 Last_Output_Torque;
	F32 Max_Output_Torque_Step;

	ParamID_Type  PC_Read_ID;
	
	
	U8 DisEnable_To_Discharge_Flag;
	U16 Discharge_Cnt;

	U8 Enable_Manul_Ctrl_Torque_Flag;
    F32 Manul_Ctrl_Torque;

	U8  Enable_Manul_Acc_Flag;
	S16 Manul_Acc_Vale;
} TRACTION_INNER;


typedef struct TRACTION_OUTPUT
{
	F32 Truck_Speed_mph;/*units m/h*/
	F32 Truck_Speed_mps;/*units m/s*/
	F32 Motor_Speed_rpm;/*units rev/m*/
	S16 Display_Speed;
	F32 Torque_Out;
	F32 Speed_Setpoint;
	
	U8  Enable_Left_Motor;
	U8  Enable_Left_Motor_Pwm;
	U8  Enable_Right_Motor;
	U8  Enable_Right_Motor_Pwm;

	
} TRACTION_OUTPUT;

typedef struct TRACTION
{
	TRACTION_PARAMETER 			Parameter;
	TRACTION_INPUT 				Input;
	TRACTION_INNER				Inner;
	TRACTION_OUTPUT				Output;
} TRACTION;


typedef struct
{
	U32 Input_X;
	U32 X[6];
	U32 Y[6];
	U32 Output_Y;
}INTERPOLATE_PIONT6;

/***************************************************************************************************************************************/

#ifndef __TRACTION_C__
#define TRACTION_EXTERN  extern
#else
#define TRACTION_EXTERN
#endif

//---------------------------------------------------------------------------
// Global Variable References

TRACTION_EXTERN TRACTION 		Traction;

TRACTION_EXTERN INTERPOLATE 	Accelerator_Curve;
TRACTION_EXTERN INTERPOLATE 	Brake_Curve;

TRACTION_EXTERN RMPCNTL			Speed_Ramp;
TRACTION_EXTERN PI_CONTROLLER 	pi_Brake2Torque;
TRACTION_EXTERN PI_CONTROLLER 	pi_Speed2Torque;

//---------------------------------------------------------------------------
// Function Prototypes

//public
TRACTION_EXTERN void Traction_Init(void);
TRACTION_EXTERN void Traction_Control_10ms(void);
TRACTION_EXTERN void Traction_Output_10ms(void);

//private
TRACTION_EXTERN void Traction_Parameter_Update(void);
TRACTION_EXTERN void Traction_Input_Update(void);

TRACTION_EXTERN void Accelerator2Speed(void);
TRACTION_EXTERN void Speed_Management(void);

TRACTION_EXTERN void AccelerationRamp2Speed(void);
TRACTION_EXTERN void Speed_Truck2Motor(void);
TRACTION_EXTERN void MotorSpeed2Torque(void);

TRACTION_EXTERN void ChipTemperture2Torque(void);
TRACTION_EXTERN void MotorTemperture2Torque(void);
TRACTION_EXTERN void BrakePedal2Torque(void);
TRACTION_EXTERN void Torque_Management(void);

TRACTION_EXTERN void Speed_Motor2Truck(void);
void PC_Update_Parameter(U16 *MsgData);
void PC_get_ParameterSts(U16 *TX_MsgData);

void SteerAngle2Speed(void);

void Interpolate_Piont6_Calc(INTERPOLATE_PIONT6 *p);
void Clear_Drive_Paramter(void);
void EnterBrakePreprocessParamter(void);
void EnterBrakePreprocessTorque(void);
F32  Get_Truck2Motor(F32 Speed_mph);
void BatterySOC2Torque(void);


/***************************************************************************************************************************************/

#ifdef __cplusplus
}
#endif /* extern "C" */

//---------------------------------------------------------------------------
#endif  // end of __SWI_THREAD_H__ definition
